package com.example.desinger.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.desinger.common.R;
import com.example.desinger.entity.StuHomeWork;
import com.example.desinger.mapper.StuHomeWorkMapper;
import com.example.desinger.service.StuHomeWorkService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StuHomeWorkServiceImpl extends ServiceImpl<StuHomeWorkMapper, StuHomeWork> implements StuHomeWorkService {
    @Override
    public R<String> updateScoreById(StuHomeWork stuHomeWork) {
        //查找数据
        System.out.println("传来的数据"+stuHomeWork);
        LambdaQueryWrapper<StuHomeWork> stuHomeWorkLambdaQueryWrapper  =
                new LambdaQueryWrapper<>();
        stuHomeWorkLambdaQueryWrapper.eq(StuHomeWork::getClassId,stuHomeWork.getClassId())
                .eq(StuHomeWork::getStuId,stuHomeWork.getStuId())
                .eq(StuHomeWork::getHomeWorkId,stuHomeWork.getHomeWorkId());
            StuHomeWork one = getOne(stuHomeWorkLambdaQueryWrapper);
            if (one==null){
                return R.error("作业不存在");
            }

        // 构建更新条件
        LambdaUpdateWrapper<StuHomeWork> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(StuHomeWork::getScore, stuHomeWork.getScore())
                .set(StuHomeWork::getState,2)
                .eq(StuHomeWork::getClassId,stuHomeWork.getClassId())
                .eq(StuHomeWork::getStuId,stuHomeWork.getStuId())
                .eq(StuHomeWork::getHomeWorkId,stuHomeWork.getHomeWorkId());

        boolean update = update(null, updateWrapper);
        if (update){
            return R.success("修改成功");
        }
        return R.error("修改失败，未知错误");
    }

    //学生查询自己所有的文件
    @Override
    public R<List<StuHomeWork>> selectHWById(int classId, int stuId) {
         LambdaQueryWrapper<StuHomeWork> lambdaQueryWrapper =
                 new LambdaQueryWrapper<>();
         lambdaQueryWrapper.eq(StuHomeWork::getClassId,classId)
                 .eq(StuHomeWork::getStuId,stuId);

        System.out.println("直接打印出来的："+list(lambdaQueryWrapper));

        List<StuHomeWork> list = list(lambdaQueryWrapper);

        System.out.println("查出来的LIST的集合为："+list);

        if (list.isEmpty()){
          return   R.error("此学生没有作业的数据");
        }

        return R.success(list,"查询成功");
    }

    @Override
    public R<String> upDateSelf(StuHomeWork stuHomeWork) {
            LambdaUpdateWrapper<StuHomeWork> sl = new LambdaUpdateWrapper<>();
            sl.set(StuHomeWork::getInterfilesId,stuHomeWork.getInterfilesId())
                    .eq(StuHomeWork::getClassId,stuHomeWork.getClassId())
                    .eq(StuHomeWork::getStuId,stuHomeWork.getStuId())
                    .eq(StuHomeWork::getId,stuHomeWork.getId());
        boolean update = update(null, sl);
        if (update){
            return R.success("标签修改成功！");
        }
        return R.error("标签修改失败！");
    }


}
